﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcFlow.Mvc
{
    [Serializable]
    public class Url
    {
        private readonly Uri                    uri;
        private readonly HttpValueDictionary    queryString;
        private readonly string[]               pathSegments;
        
        public string               Value
        {
            get { return uri.OriginalString; }
        }

        public string[]             PathSegments
        {
            get { return pathSegments; }
        }

        public HttpValueDictionary  QueryString
        {
            get { return queryString; }
        }

        public Url( Uri uri )
        {
            this.uri                = uri;
            var queryStringValues   = HttpUtility.ParseQueryString(uri.Query);
            queryString             = new HttpValueDictionary(queryStringValues);
            pathSegments            = uri.Segments.Select(s => s.Trim().Trim('/')).Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
        }
    }
}